<?php

	require_once 'View/FileUploadView.php';
	
	class FileUploadController{
		private $m_loginHandler = NULL;
		private $SaveFileFolder = "uploads/";
		private $fileUploadView;
		private $fileUploadModel;
		private $xhtml = "";
		
		public function __construct(LoginHandler $loginHandler){
			$this->m_loginHandler = $loginHandler;
			$this->fileUploadView = new FileUploadView();
			$this->fileUploadModel = new FileUploadModel();
		}
		public function DoControl(){
				
			
						
			// Är användaren inloggad?
			if ($this->m_loginHandler->IsLoggedIn()) {
				
				// Om användaren har klickat på 'Ladda upp'
				if ($this->fileUploadView->TriedToUpload() == TRUE) {
					
					// Om fil finns är vald
					if($this->fileUploadView->HasFile() == TRUE){
						
						//.. spara filen
						if($this->fileUploadModel->SaveFile($this->fileUploadView->GetFileInfo()) == FALSE){
							$this->xhtml .= $this->fileUploadView->uploadFailed();
						}else{
							$this->xhtml .= $this->fileUploadView->uploadSucceeded();
						}
					}
				}
				$this->xhtml .= $this->fileUploadView->DoUploadForm();
			}
			$this->xhtml .= $this->fileUploadView->ShowFiles($this->fileUploadModel->GetFiles());
			
			$page = new Page();
			$page->body = $this->xhtml;
			
			
			return $page;					
		}
		


 	}
